import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 苏李涛
 * Date: 2024-07-14
 * Time: 16:47
 */
public class Test {
    public static void main(String[] args) {

        List<Integer> list = new LinkedList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);

        System.out.println(list);

        ArrayList<Integer> list1 = new ArrayList<>();
        list1.add(11);
        list1.add(12);
        list1.add(13);

        System.out.println("==============");
        list.addAll(list1);
        System.out.println(list);
        System.out.println();






        System.out.println("foreach遍历");
        for (Integer x:list) {
            System.out.print(x + " ");
        }
        System.out.println();

        System.out.println("迭代器遍历历");
        Iterator<Integer> it = list.iterator();
        while (it.hasNext()) {
            System.out.print(it.next() + " ");
        }

        /**
         * ListIterator是Iterator的一个子类，可以专门用来打印链表
         */

        System.out.println();
        System.out.println("使用迭代器遍历---正向遍历");
        ListIterator<Integer> it1 = list.listIterator();
        while (it1.hasNext()) {
            System.out.print(it1.next() + " ");
        }



        System.out.println();
        System.out.println("使用反向迭代器---反向遍历");

        ListIterator<Integer> it2 = list.listIterator(/*这里要传链表的长度*/ list.size());
        while (it2.hasPrevious()) {
            System.out.print(it2.previous() + " ");
        }
    }
}
